package jlfm.lastfm.apps.recorder;

import jlfm.lastfm.apps.ID3MetadataProvider;
import jlfm.lastfm.client.TrackMetadata;
import jlfm.lastfm.tools.id3.ID3Metadata;

public class ID3FileNameProvider extends SimplePatternFileNameProvider {

	private ID3MetadataProvider id3MetadataProvider;

	public ID3FileNameProvider(String fileNamePattern,
			ID3MetadataProvider id3MetadataProvider) {
		super(fileNamePattern);
		this.id3MetadataProvider = id3MetadataProvider;
	}

	@Override
	public String getOutputFileName(TrackMetadata m) {
		try {
			ID3Metadata id3Metadata = id3MetadataProvider.getID3Metadata(m);
			if (id3Metadata != null) {
				return super.getOutputFileName(id3Metadata.getArtistName(),
						id3Metadata.getAlbumTitle(), id3Metadata
								.getTrackTitle(), m.getStationName());
			}else{
				return super.getOutputFileName(m);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			return super.getOutputFileName(m);
		}
	}

}
